package com.edu.service.impl;

import com.edu.javaBean.Invoice;
import com.edu.javaBean.PatientCosts;
import com.edu.javaBean.Register;
import com.edu.javaBean.User;
import com.edu.mapper.InvoiceMapper;
import com.edu.mapper.PatientCostsMapper;
import com.edu.mapper.RegisterMapper;
import com.edu.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    private RegisterMapper registerMapper;
    @Autowired
    private InvoiceMapper invoiceMapper;
    @Autowired
    private PatientCostsMapper patientCostsMapper;
    public String getMaxCaseNum() {
        return registerMapper.getMaxCaseNum();
    }

    @Override
    public int getExsitsQuota(String visitedate, String noon, int userid) {
        return registerMapper.getExsitsQuota(visitedate,noon,userid);
    }

    @Override
    public List<Register> getWaitingRegs(Integer id) {
        return registerMapper.getWaitingRegs(id);
    }

    @Override
    @Transactional
    public int saveRegister(Map<String, Object> map, HttpSession session) {
        //挂号，1.插入register，2.插入invoi3.插入patientCosts
        SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
        Date d=new Date();
        String date=sf.format(d);
        Date d1=null;
        try{
            d1=sf.parse(map.get("visitdate").toString());
        }catch (Exception ex){
            System.out.println(ex.getMessage());
        }
        Register reg=new Register();
        reg.setCasenumber(map.get("casenumber").toString());
        reg.setRealname((String)map.get("realname"));
        reg.setGender(Integer.parseInt(map.get("gender").toString()));
        reg.setDeptid((int)map.get("deptid"));
        reg.setVisitdate(d1);
        reg.setNoon((String)map.get("noon"));
        reg.setUserid((int)map.get("userid"));
        reg.setRegisttime(new Timestamp(new Date().getTime()));
        reg.setVisitstate(1);
        reg.setRegistleid((int)map.get("registleid"));
        User u=(User)session.getAttribute("userInfo");
        reg.setRegisterid(u.getId());
        reg.setSettleid((int)map.get("settleid"));
        registerMapper.insertSelective(reg);
        int regId=reg.getId();

        Invoice inv=new Invoice();
        inv.setInvoicenum(map.get("invoicenum").toString());
        inv.setFeetype(Integer.parseInt(map.get("settleid").toString()));
        inv.setState(1);
        inv.setCreationtime(new Timestamp(new Date().getTime()));
        inv.setMoney(Long.parseLong(map.get("money").toString()));
        inv.setUserid(u.getId());
        inv.setRegistid(regId);
        inv.setFeetype((int)map.get("settleid"));
        inv.setDailystate(0);
        invoiceMapper.insertSelective(inv);
        int inId=inv.getId();

        PatientCosts ps=new PatientCosts();
        ps.setInvoiceid(inId);
        ps.setRegistid(regId);
        ps.setItemid(1);
        ps.setItemtype(1);
        ps.setName("挂号费");
        ps.setPrice(Long.parseLong(map.get("money").toString()));
        ps.setAmount(new BigDecimal(1));
        ps.setDeptid((int)map.get("deptid"));
        ps.setCreatetime(new Timestamp(new Date().getTime()));
        ps.setCreateoperid(u.getId());
        ps.setPaytime(new Timestamp(new Date().getTime()));
        ps.setRegisterid(u.getId());
        ps.setFeetype((int)map.get("settleid"));
        patientCostsMapper.insertSelective(ps);

        return ps.getId();
    }
}
